--This is a Relay Library to allow the Addon to function without RD2 or Wire installed

--WIRE RELAY FUNCTIONS
function CDE_FindContraption(entity)
	if (AdvDupe != nil) then
		return AdvDupe.GetAllEnts( entity, {}, {}, {})
	else
		duplicator.GetAllConstrainedEntitiesAndConstraints( entity, {}, {})
	end
end

function CDE_Wire_CreateInputs(entity, inputs)
	if WireAddon or false then
		return Wire_CreateInputs(entity, inputs)
	end
end

function CDE_Wire_CreateSpecialInputs(entity, inputs, inputs2)
	if WireAddon or false then
		return WireLib.CreateSpecialInputs(entity, inputs, inputs2)
	end
end

function CDE_Wire_CreateOutputs(entity, outputs)
	if WireAddon or false then
		return Wire_CreateOutputs(entity, outputs)
	end
end

function CDE_Wire_TriggerOutput(entity, output, value)
	if WireAddon or false then
		return Wire_TriggerOutput(entity, output, value)
	end
end


--RESOURCE DISTRIBUTION RELAY FUNCTIONS
--[[
	These functions manage the detection of the currently-installed version of RD, and facilitate calling the relevant functions depending on the installed version
	In cases where both RD2 and RD3 are installed, the relays will default to using RD2
]]--
local function CDE_RD_GetVersion()
	if util.tobool(RD2Version) then
		return 2
	elseif util.tobool(CAF) and util.tobool(CAF.GetAddon("Resource Distribution")) then
		return 3
	else
		return false
	end
end

function CDE_RD_AddResource(entity, resource, amount)
	local RDVersion = CDE_RD_GetVersion()
	if RDVersion == 2 then
		return RD_AddResource(entity, resource, amount)
	elseif RDVersion == 3 then
		return CAF.GetAddon("Resource Distribution").AddResource(entity, resource, 0, 0)
	else
		return 0
	end
end

function CDE_RD_GetResourceAmount(entity, resource, isweapon)
	local RDVersion = CDE_RD_GetVersion()
	if (isweapon and !util.tobool(GetConVarNumber("sv_CDamage_weapons_RDenabled"))) or (not util.tobool(RDVersion)) then
		return 1000000
	elseif RDVersion == 2 then
		return RD_GetResourceAmount(entity, resource)
	elseif RDVersion == 3 then
		return CAF.GetAddon("Resource Distribution").GetResourceAmount(entity, resource)
	else
		return 0
	end
end

function CDE_RD_ConsumeResource(entity, resource, amount, isweapon)
	local RDVersion = CDE_RD_GetVersion()
	if (isweapon and !util.tobool(GetConVarNumber("sv_CDamage_weapons_RDenabled"))) or (not util.tobool(RDVersion)) then
		return 0
	elseif RDVersion == 2 then
		return RD_ConsumeResource(entity, resource, amount)
	elseif RDVersion == 3 then
		return CAF.GetAddon("Resource Distribution").ConsumeResource(entity, resource, amount)
	end
end

function CDE_RD_BuildDupeInfo(entity)
	local RDVersion = CDE_RD_GetVersion()
	if RDVersion == 2 then
		RD_BuildDupeInfo(entity)
	elseif RDVersion == 3 then
		CAF.GetAddon("Resource Distribution").BuildDupeInfo(entity)
	end
end

function CDE_RD_ApplyDupeInfo(Ent, CreatedEntities)
	local RDVersion = CDE_RD_GetVersion()
	if RDVersion == 2 then
		RD_ApplyDupeInfo(Ent, CreatedEntities)
	elseif RDVersion == 3 then
		CAF.GetAddon("Resource Distribution").ApplyDupeInfo(Ent, CreatedEntities)
	end
end